package hotel.controller.user;

import javafx.fxml.FXML;
import javafx.scene.control.Button;

import javafx.scene.control.TextField;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import javafx.util.Callback;
import javafx.util.converter.IntegerStringConverter;

import java.util.ArrayList;

import hotel.common.Md5;
import hotel.common.MsgBox;
import hotel.discard.EmployeeSql;
import hotel.modal.Employee;
import hotel.modal.User;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.control.ChoiceBox;

public class UserAddController {
	@FXML private TableView<Employee> tableView;
	@FXML private TableColumn<Employee, Integer> employeeIdCol;
	@FXML private TableColumn<Employee, String> employeeNameCol;
	@FXML private TextField employeeIdField;
	@FXML private ChoiceBox<String> adminCh;
	@FXML private TextField userNameField;
	@FXML private PasswordField passwordField;
	@FXML private PasswordField passwordField2;
	@FXML private Button btnReset;
	@FXML private Button btnSave;
	@FXML private Button btnCancel;
	User user = null;
	
	@FXML
	public void initialize() {
		
		employeeIdCol.setCellFactory(TextFieldTableCell.<Employee, Integer>forTableColumn(new IntegerStringConverter()));

		tableView.setRowFactory(new Callback<TableView<Employee>, TableRow<Employee>>() {
			@Override
			public TableRow<Employee> call(TableView<Employee> tableView) {
				final TableRow<Employee> row = new TableRow<>();
				row.setOnMouseClicked(new EventHandler<MouseEvent>() {
					@Override
					public void handle(MouseEvent event) {
						Employee employee = row.getItem();
						if(employee == null) return;
						String str = null;
						int id = employee.getEmployeeId();
						if (id != -1)
							str = String.valueOf(id);
						
						employeeIdField.setText(str);
					}
				});
				return row;
			}
		});
		
		// 读取初始列表
		loadInitInfo();
		
		// 获取焦点
		userNameField.requestFocus(); 
	}
	
	public void loadInitInfo() {
		ObservableList<Employee> data = tableView.getItems();
		ArrayList<Employee> employeeList = EmployeeSql.getAll();
		Employee[] employees = new Employee[employeeList.size()];
		employeeList.toArray(employees);
		data.addAll(employees);
		tableView.refresh();
		//if(data.size() > 0) {
			//tableView.getSelectionModel().select(0);
			//employeeIdField.setText(String.valueOf((tableView.getSelectionModel().getSelectedItem().getEmployeeId())));
		//}
	}
	
	public void setInitData(User user) {
		this.user = user;
	}

	// Event Listener on Button[#btnReset].onAction
	@FXML
	public void onBtnReset(ActionEvent event) {
		userNameField.clear();
		employeeIdField.clear();
		passwordField.clear();
		passwordField2.clear();
		adminCh.getSelectionModel().select(0);
		
	}
	
	// Event Listener on Button[#btnSave].onAction
	@FXML
	public void onBtnSave(ActionEvent event) {
		Boolean pass = true;
		int employeeId;
		String userName = userNameField.getText().trim();
		String stremployeeId = employeeIdField.getText();
		if(stremployeeId != null && !stremployeeId.equals(""))
			employeeId = Integer.parseInt(stremployeeId);
		else employeeId = -1;
		int admin = 0;
		String password = "";
		
		if(userName.length() > 20) {
			pass = false;
			MsgBox.showErrorDialog("错误", "登录名过长!", "请检查并重新输入用户名。");
			userNameField.clear();		  // 清空内容
			userNameField.requestFocus(); // 获取焦点
		}
		
		if(adminCh.getSelectionModel().getSelectedItem().equals("是")) admin = 1;
		if(passwordField.getText().equals("")) {
			pass = false;
			MsgBox.showErrorDialog("错误", "密码不能为空!", "请检查并重新输入密码。");
			passwordField.requestFocus();
		}
		if(passwordField.getText().equals(passwordField2.getText())) {
			password = passwordField.getText();
		} else {
			pass = false;
			MsgBox.showErrorDialog("错误", "两次输入的密码不一致!", "请检查并重新输入密码。");
			passwordField.clear();
			passwordField2.clear();
			passwordField.requestFocus();
		}
		if (pass) {
			user.setUserName(userName);
			user.setEmployeeId(employeeId);
			user.setPassword(Md5.getMD5(password));
			user.setAdmin(admin > 0 ? true : false);
			user.setModified(true);
			
			closeWindow();
		}else pass = true;
	}
	// Event Listener on Button[#btnCancel].onAction
	@FXML
	public void onBtnCancel(ActionEvent event) {
		user.setModified(false);
		closeWindow();
	}
	
	private void closeWindow() {
		Stage stage = (Stage) btnCancel.getScene().getWindow();
		stage.close();
	}
}
